﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BF.Model;
using BF.IBLL.BaseSystem;
using Base.Core.UnityFactory;
using BF.Infrastructure;
using System.Web.UI.HtmlControls;

namespace BF.Web.pages.BaseSystem.FunctionManagement
{
    public partial class FunctionList : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                //LoadTree();
            }

        }

        protected void AspNetPager1_PageChanged(object sender, EventArgs e)
        {
            BindList(AspNetPager1.PageSize, AspNetPager1.CurrentPageIndex);
        }


        public void BindList(int pageSize, int currentPageIndex)
        {

            Isys_FunctionBLL iBLL = 
                IOCFactory.GetIOCResolve<Isys_FunctionBLL>();
            AspNetPager1.RecordCount = iBLL.GetRowCount(
                P => (!P.gParentKey.HasValue) ? false :
                    P.gParentKey.Value.Equals(new Guid(hidParentID.Value)));

            var list = iBLL.GetList(
                P => (!P.gParentKey.HasValue) ? false : 
                    P.gParentKey.Value.Equals(new Guid(hidParentID.Value)),
                P => new { P.iSort, P.iType },
                new PageModel(currentPageIndex), true);

            this.Repeater1.DataSource = list;
            this.Repeater1.DataBind();
        }

        protected void btnAdd_Click1(object sender, EventArgs e)
        {
        }


        protected void lbtnDelete_Click(object sender, EventArgs e)
        {
            Isys_FunctionBLL iBLL = IOCFactory.GetIOCResolve<Isys_FunctionBLL>();
            List<string> delList = new List<string>();
            for (int i = 0; i < Repeater1.Items.Count; i++)
            {
                HtmlInputCheckBox cb = (HtmlInputCheckBox)Repeater1
                    .Items[i].FindControl("chxKey");
                if (cb != null && cb.Checked)
                {
                    delList.Add(cb.Value);
                }
            }
            if (iBLL.Delete(delList))
                Messages("操作成功!");
            else
                Messages("操作失败!");

            this.AspNetPager1.CurrentPageIndex = 1;
        }

        protected void lbtnUp_Click(object sender, EventArgs e)
        {
            Isys_FunctionBLL iBLL = 
                IOCFactory.GetIOCResolve<Isys_FunctionBLL>();
            List<string> delList = new List<string>();
            for (int i = 0; i < Repeater1.Items.Count; i++)
            {
                HtmlInputCheckBox cb = (HtmlInputCheckBox)Repeater1
                    .Items[i].FindControl("chxKey");
                if (cb != null && cb.Checked)
                {
                    int iSort = int.Parse(cb.Attributes["accesskey"]);
                    iBLL.Up(cb.Value, iSort);
                    this.AspNetPager1.CurrentPageIndex = 1;
                    return;
                }
            }

        }

        protected void lbtnDown_Click(object sender, EventArgs e)
        {
            Isys_FunctionBLL iBLL = IOCFactory.GetIOCResolve<Isys_FunctionBLL>();
            List<string> delList = new List<string>();
            for (int i = 0; i < Repeater1.Items.Count; i++)
            {
                HtmlInputCheckBox cb = (HtmlInputCheckBox)Repeater1
                    .Items[i].FindControl("chxKey");
                if (cb != null && cb.Checked)
                {
                    int iSort = int.Parse(cb.Attributes["accesskey"]);
                    iBLL.Down(cb.Value, iSort);
                    this.AspNetPager1.CurrentPageIndex = 1;
                    return;
                }
            }
        }
    }
}